<template>
  <div>
    <van-nav-bar :title="receiverName" left-text="返回" left-arrow @click-left="() => $router.go(-1)" />
    <div>
      <div v-for="(item, index) in certainChatHistory" :key="index">
        <div v-if="item.sender !== myid" class="notme">
          <div>
            <img :src="item.senderImg" width="50px" height="50px" />
          </div>
          <div class="otherMess">
            <div class="content">{{ item.content }}</div>
          </div>
        </div>
        <div v-else class="isme">
          <div>
            <img :src="item.senderImg" width="50px" height="50px" />
          </div>
          <div class="myMess">
            <div class="content">{{ item.content }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="foot">
      <van-field v-model="inputWords" center placeholder="请输入..." @keyup.enter.native="send">
        <template #button>
          <van-button
            v-if="inputWords"
            size="small"
            type="primary"
            @click="send"
            >发送</van-button
          >
          <van-icon
            v-else
            name="add-o"
            size="6vw"
            @click="showPanel = !showPanel"
          />
        </template>
      </van-field>
      <div class="panel" v-if="showPanel"></div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  sockets: {
    updateChatMessageList(data) {
      this.UPDATE_CHAT_HISTORY(data);
    },
  },
  data() {
    return {
      myid:'',
      inputWords: '',
      receiver:'',
      receiverName:'',
      receiverImg:'',
      showPanel: false,
      certainChatHistory:[]
    };
  },
  created(){
    this.myid=localStorage.getItem('userID')
    this.receiverName=this.$route.query.name
    this.receiver=this.$route.query.userID
    this.receiverImg=this.$route.query.avator
    this.chatUserList.forEach(item=>{
      if(item.userID.toString()===this.receiver){
        this.certainChatHistory=item.historyList
      }
    })
  },
  methods: {
    ...mapMutations(["UPDATE_CHAT_HISTORY"]),
    send() {
      let data = {
        sender: this.myid,
        senderImg: localStorage.getItem('avator'),
        receiver: this.receiver,
        content: this.inputWords,
      };
      this.$socket.emit("privateChat", data);
      this.inputWords = "";
      this.UPDATE_CHAT_HISTORY(data);
      this.$axios.post('/saveChat',data)
    },
  },

  mounted() {
    this.$socket.emit("login", {
      name: localStorage.getItem("userID"),
    });
  },
  computed: {
    ...mapState(["chatUserList"]),
  },
};
</script>

<style scoped>
.head {
  font-size: 20px;
  font-weight: bold;
  padding:10px;
  background-color: #FAFAFA;
}
.foot {
  width: 100vw;
  position: fixed;
  bottom: 0;
}
.panel {
  width: 100vw;
  height: 20vh;
}
.van-cell {
  background-color: #fafafa;
}
.myMess,.otherMess{
  border-radius: 5px;
  margin:0 5px;
  padding:5px;
  align-items: center;
  text-align: left;
  display: flex;
  align-items: center;
}
.myMess {
  background-color: orange;
}
.otherMess{
  background-color: #fafafa;
}
.isme,.notme{
    display: flex;
    padding: 5px;
}
.notme {
  flex-direction: row;
}
.isme {
  flex-direction: row-reverse;
}
</style>